package com.ifonly.mvc;

import com.ifonly.config.Configuration;
import com.ifonly.interceptor.Interceptor;
import com.ifonly.reports.Reporter;
import com.ifonly.upload.MultipartServletRequest;
import com.ifonly.utils.ArrayUtils;
import com.ifonly.utils.DateTimeUtils;
import com.ifonly.utils.RequestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-11 15:35
 * @since JDK 1.6
 */
class ActionReporter {

    private static Configuration instance = Configuration.getInstance();
    private static Reporter reporter;

    public static void report(HttpServletRequest request, Action action, String[] params) {
        Class<? extends Controller> controllerClass = action.getControllerClass();

        StringBuilder sb = new StringBuilder();
        sb.append("Action Report--------- ").append(DateTimeUtils.format(new Date())).append(" ----------------------------------------------------")
                .append("\r\n")
                .append("Request url : ").append(action.getActionUrl()).append("\r\n")
                .append("Controller : ").append(controllerClass.getName())
                .append(".(").append(controllerClass.getSimpleName()).append(".java:1)").append("\r\n")
                .append("Method : ").append(action.getMethodName()).append("\r\n");

        if (!ArrayUtils.isNullOrEmpty(params)) {
            sb.append("Path params: ").append(Arrays.toString(params)).append("\r\n");
        }

        Interceptor[] inters = action.getInterceptors();
        if (inters.length > 0) {
            sb.append("Interceptor : ");
            for (int i=0; i<inters.length; i++) {
                if (i > 0)
                    sb.append("\rn              ");
                Interceptor inter = inters[i];
                Class<? extends Interceptor> ic = inter.getClass();
                sb.append(ic.getName()).append(".(").append(ic.getSimpleName()).append(".java:1)");
            }
            sb.append("\r\n");
        }

        Map<String, String[]> parameterMap = request.getParameterMap();
        if (!parameterMap.isEmpty()) {
            sb.append("Parameter: ").append("{");
            Set<Map.Entry<String, String[]>> entrySet = parameterMap.entrySet();
            int len = entrySet.size() - 1;
            int index = 0;
            for (Map.Entry<String, String[]> entry: entrySet) {
                String key = entry.getKey();
                String[] value = entry.getValue();
                sb.append(key).append(" = ");
                if (value.length > 1) {
                    sb.append(Arrays.toString(value));
                } else {
                    sb.append(value[0]);
                }
                if (index != len) {
                    sb.append(", ");
                    index++;
                }
            }
            sb.append("}").append("\r\n");
        }
        if (RequestUtils.isMultipart(request) && request instanceof MultipartServletRequest) {
            MultipartServletRequest multipartServletRequest = (MultipartServletRequest) request;
            Enumeration<String> enumeration = multipartServletRequest.getFileNames();
            sb.append("Files: ").append("{\r\n");
            while (enumeration.hasMoreElements()) {
                String nextElement = enumeration.nextElement();
                sb.append("\t").append(nextElement).append(" = ")
                        .append(Arrays.toString(multipartServletRequest.getFiles(nextElement)))
                        .append("\r\n");
            }
            sb.append("}").append("\r\n");
        }

        sb.append("--------------------------------------------------------------------------------\r\n");
        sb.append("\r\n");
        if (ActionReporter.reporter == null) {
            reporter = instance.getReporter();
        }
        reporter.report(sb.toString());
    }

}
